/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bungabungaball.gamestates;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.gui.MouseOverArea;
import org.newdawn.slick.gui.TextField;
import org.newdawn.slick.Color;
import org.newdawn.slick.Image;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.FileReader;
import java.io.IOException;
import bungabungaball.gamestates.menu.*;

/**
 * The class LevelEditor enables creation of new maps. Furthermore there are
 * options to save and load maps.
 *
 * @author Ingo Klose, Stephan Meyer, Sebastian Lehmann
 * @version 1.0
 */
public class LevelEditor extends BasicGameState {

    /**
     * The stateID
     */
    int stateID = 1;
    /**
     * The centre X
     */
    float mittelpunktX = 408.f;
    /**
     * The centre Y
     */
    float mittelpunktY = 384.f;
    /**
     * The field length
     */
    int fieldSizeX = 600;
    /**
     * The field height
     */
    int fieldSizeY = 400;
    /**
     * The goalsize
     */
    int fieldGoalSize = 200;
    /**
     * The frequency of powerUps
     */
    int powerUp = 50;
    /**
     * The frequency of powerDowns
     */
    int powerDown = 50;
    /**
     * The backgroundcolor
     */
    private Color background;
    /**
     * The linecolor
     */
    private Color lineColor;
    /**
     * The type of baricade
     */
    private boolean[] hinderniss = {false, false, false, false, false};
    /**
     * The array map saves the position of baricades
     */
    private int[][] map;
    /**
     * The array recognizes the state during safes 1. successful 2. no filename
     * 3. error
     */
    private boolean[] speicherFehler = {false, false, false}; // 1. erfolgreich, 2. kein Dateiname, 3. Fehler
    /**
     * Includes the mode false = Editor Mode true = Load Mode
     */
    private boolean mode = false;
    /**
     * An image
     */
    private Image wall1x1;
    /**
     * An image
     */
    private Image wall2x1;
    /**
     * An image
     */
    private Image wall1x2;
    /**
     * An image
     */
    private Image wall2x2;
    /**
     * An image
     */
    private Image eraserIcon;
    /**
     * MouseOverArea
     */
    private MouseOverArea minus1;
    /**
     * MouseOverArea
     */
    private MouseOverArea minus2;
    /**
     * MouseOverArea
     */
    private MouseOverArea minus3;
    /**
     * MouseOverArea
     */
    private MouseOverArea minus4;
    /**
     * MouseOverArea
     */
    private MouseOverArea minus5;
    /**
     * MouseOverArea
     */
    private MouseOverArea plus1;
    /**
     * MouseOverArea
     */
    private MouseOverArea plus2;
    /**
     * MouseOverArea
     */
    private MouseOverArea plus3;
    /**
     * MouseOverArea
     */
    private MouseOverArea plus4;
    /**
     * MouseOverArea
     */
    private MouseOverArea plus5;
    /**
     * MouseOverArea
     */
    private MouseOverArea wallY1x1;
    /**
     * MouseOverArea
     */
    private MouseOverArea wallY2x2;
    /**
     * MouseOverArea
     */
    private MouseOverArea wallY1x2;
    /**
     * MouseOverArea
     */
    private MouseOverArea wallY2x1;
    /**
     * MouseOverArea
     */
    private MouseOverArea colorwhite;
    /**
     * MouseOverArea
     */
    private MouseOverArea colorblue;
    /**
     * MouseOverArea
     */
    private MouseOverArea colorred;
    /**
     * MouseOverArea
     */
    private MouseOverArea coloryellow;
    /**
     * MouseOverArea
     */
    private MouseOverArea colorgreen;
    /**
     * MouseOverArea
     */
    private MouseOverArea colorblack;
    /**
     * MouseOverArea
     */
    private MouseOverArea colorpink;
    /**
     * MouseOverArea
     */
    private MouseOverArea colorgray;
    /**
     * MouseOverArea
     */
    private MouseOverArea eraser;
    /**
     * MouseOverArea
     */
    private MouseOverArea save;
    /**
     * MouseOverArea
     */
    private MouseOverArea newMap;
    /**
     * MouseOverArea
     */
    private MouseOverArea load;
    /**
     * Textfield to enter the name of the map
     */
    private TextField textField;
    /**
     * The position x of the mouse
     */
    private int mouseX;
    /**
     * The position y of the mouse
     */
    private int mouseY;
    /**
     * The position x on the map
     */
    int mapX;
    /**
     * The position y on the map
     */
    int mapY;
    /**
     * MenyEntry to get back
     */
    private MenuEntry back;
    /**
     * MenuEntry for all saved maps
     */
    private MenuEntry MapEntries[];
    /**
     * Includes all names of saved maps
     */
    private File files[];

    /**
     * Constructor
     *
     * @param stateID
     */
    public LevelEditor(int stateID) {
        this.stateID = stateID;
        this.fillDefaultMap();

    }

    /**
     * Get stateID
     *
     * @return Returns stateID
     */
    @Override
    public int getID() {
        return this.stateID;
    }

    /**
     * Initialize the LevelEditor
     *
     * @param gc
     * @param sbg
     * @throws SlickException
     */
    @Override
    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {

        background = new Color(Color.white);
        lineColor = new Color(Color.black);
        MapEntries = new MenuEntry[30];
        wall1x1 = new Image("\\img\\wall_yellow1x1.png");
        wall2x1 = new Image("\\img\\wall_yellow2x1.png");
        wall1x2 = new Image("\\img\\wall_yellow1x2.png");
        wall2x2 = new Image("\\img\\wall_yellow2x2.png");
        eraserIcon = new Image("\\img\\eraser.png");
        minus1 = new MouseOverArea(gc, new Image("\\img\\minus.png"), 850, 50);
        minus1.setMouseOverColor(Color.lightGray);
        minus1.setNormalColor(Color.white);
        minus1.setMouseDownColor(Color.red);
        minus2 = new MouseOverArea(gc, new Image("\\img\\minus.png"), 850, 125);
        minus2.setMouseOverColor(Color.lightGray);
        minus2.setNormalColor(Color.white);
        minus2.setMouseDownColor(Color.red);
        minus3 = new MouseOverArea(gc, new Image("\\img\\minus.png"), 850, 200);
        minus3.setMouseOverColor(Color.lightGray);
        minus3.setNormalColor(Color.white);
        minus3.setMouseDownColor(Color.red);
        minus4 = new MouseOverArea(gc, new Image("\\img\\minus.png"), 850, 275);
        minus4.setMouseOverColor(Color.lightGray);
        minus4.setNormalColor(Color.white);
        minus4.setMouseDownColor(Color.red);
        minus5 = new MouseOverArea(gc, new Image("\\img\\minus.png"), 850, 350);
        minus5.setMouseOverColor(Color.lightGray);
        minus5.setNormalColor(Color.white);
        minus5.setMouseDownColor(Color.red);
        plus1 = new MouseOverArea(gc, new Image("\\img\\plus.png"), 950, 50);
        plus1.setMouseOverColor(Color.lightGray);
        plus1.setNormalColor(Color.white);
        plus1.setMouseDownColor(Color.green);
        plus2 = new MouseOverArea(gc, new Image("\\img\\plus.png"), 950, 125);
        plus2.setMouseOverColor(Color.lightGray);
        plus2.setNormalColor(Color.white);
        plus2.setMouseDownColor(Color.green);
        plus3 = new MouseOverArea(gc, new Image("\\img\\plus.png"), 950, 200);
        plus3.setMouseOverColor(Color.lightGray);
        plus3.setNormalColor(Color.white);
        plus3.setMouseDownColor(Color.green);
        plus4 = new MouseOverArea(gc, new Image("\\img\\plus.png"), 950, 275);
        plus4.setMouseOverColor(Color.lightGray);
        plus4.setNormalColor(Color.white);
        plus4.setMouseDownColor(Color.green);
        plus5 = new MouseOverArea(gc, new Image("\\img\\plus.png"), 950, 350);
        plus5.setMouseOverColor(Color.lightGray);
        plus5.setNormalColor(Color.white);
        plus5.setMouseDownColor(Color.green);
        wallY1x1 = new MouseOverArea(gc, new Image("\\img\\wall_yellow1x1.png"), 850, 425);
        wallY1x1.setMouseOverColor(Color.lightGray);
        wallY1x1.setNormalColor(Color.white);
        wallY1x1.setMouseDownColor(Color.yellow);
        wallY1x2 = new MouseOverArea(gc, new Image("\\img\\wall_yellow1x2.png"), 934, 425);
        wallY1x2.setMouseOverColor(Color.lightGray);
        wallY1x2.setNormalColor(Color.white);
        wallY1x2.setMouseDownColor(Color.yellow);
        wallY2x1 = new MouseOverArea(gc, new Image("\\img\\wall_yellow2x1.png"), 880, 425);
        wallY2x1.setMouseOverColor(Color.lightGray);
        wallY2x1.setNormalColor(Color.white);
        wallY2x1.setMouseDownColor(Color.yellow);
        wallY2x2 = new MouseOverArea(gc, new Image("\\img\\wall_yellow2x2.png"), 964, 425);
        wallY2x2.setMouseOverColor(Color.lightGray);
        wallY2x2.setNormalColor(Color.white);
        wallY2x2.setMouseDownColor(Color.yellow);
        eraser = new MouseOverArea(gc, new Image("\\img\\eraser.png"), 964, 500);
        eraser.setMouseOverColor(Color.lightGray);
        eraser.setNormalColor(Color.white);
        eraser.setMouseDownColor(Color.yellow);
        save = new MouseOverArea(gc, new Image("\\img\\save.png"), 850, 620);
        save.setMouseOverColor(Color.lightGray);
        save.setNormalColor(Color.white);
        save.setMouseDownColor(Color.yellow);
        newMap = new MouseOverArea(gc, new Image("\\img\\new.png"), 850, 690);
        newMap.setMouseOverColor(Color.lightGray);
        newMap.setNormalColor(Color.white);
        newMap.setMouseDownColor(Color.yellow);
        load = new MouseOverArea(gc, new Image("\\img\\load.png"), 900, 690);
        load.setMouseOverColor(Color.lightGray);
        load.setNormalColor(Color.white);
        load.setMouseDownColor(Color.yellow);
        colorwhite = new MouseOverArea(gc, new Image("\\img\\background.png"), 850, 500);
        colorwhite.setMouseOverColor(Color.white);
        colorwhite.setNormalColor(Color.white);
        colorwhite.setMouseDownColor(Color.gray);
        colorblue = new MouseOverArea(gc, new Image("\\img\\background.png"), 874, 500);
        colorblue.setMouseOverColor(Color.blue);
        colorblue.setNormalColor(Color.blue);
        colorblue.setMouseDownColor(Color.gray);
        colorred = new MouseOverArea(gc, new Image("\\img\\background.png"), 898, 500);
        colorred.setMouseOverColor(Color.red);
        colorred.setNormalColor(Color.red);
        colorred.setMouseDownColor(Color.gray);
        coloryellow = new MouseOverArea(gc, new Image("\\img\\background.png"), 922, 500);
        coloryellow.setMouseOverColor(Color.yellow);
        coloryellow.setNormalColor(Color.yellow);
        coloryellow.setMouseDownColor(Color.gray);
        colorgreen = new MouseOverArea(gc, new Image("\\img\\background.png"), 850, 524);
        colorgreen.setMouseOverColor(Color.green);
        colorgreen.setNormalColor(Color.green);
        colorgreen.setMouseDownColor(Color.gray);
        colorblack = new MouseOverArea(gc, new Image("\\img\\background.png"), 874, 524);
        colorblack.setMouseOverColor(Color.black);
        colorblack.setNormalColor(Color.black);
        colorblack.setMouseDownColor(Color.gray);
        colorpink = new MouseOverArea(gc, new Image("\\img\\background.png"), 898, 524);
        colorpink.setMouseOverColor(Color.pink);
        colorpink.setNormalColor(Color.pink);
        colorpink.setMouseDownColor(Color.gray);
        colorgray = new MouseOverArea(gc, new Image("\\img\\background.png"), 922, 524);
        colorgray.setMouseOverColor(Color.gray);
        colorgray.setNormalColor(Color.gray);
        colorgray.setMouseDownColor(Color.darkGray);
        textField = new TextField(gc, gc.getDefaultFont(), 850, 595, 138, 20);
        textField.setFocus(true);
        this.readOutFiles(gc);


    }

    /**
     * Render the LevelEditor
     *
     * @param gc
     * @param sbg
     * @param g
     * @throws SlickException
     */
    @Override
    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {

        g.setColor(Color.black);
        g.setBackground(background);
        g.setFont(MainMenu.font);
        g.drawLine(816, 0, 816, 768);

        g.setColor(Color.lightGray);
        g.fillRect(817, 0, 208, 768);


        if (mode) {
            g.setColor(Color.black);
            g.fillRect(0, 0, 816, 768);
            g.setColor(Color.white);
            g.drawString("Map laden/ bearbeiten", 100, 100);


            this.readOutFiles(gc);
            if (files != null) { // Erforderliche Berechtigungen etc. sind vorhanden

                for (int i = 0; i < files.length; i++) {
                    g.setColor(MapEntries[i].checkMouseIn(mouseX, mouseY) ? Color.green : Color.white);
                    g.drawString(MapEntries[i].text, MapEntries[i].x, MapEntries[i].y);
                }
            } else {
                g.drawString("Keine Maps vorhanden", 100, 150);
            }


            g.setColor(back.checkMouseIn(mouseX, mouseY) ? Color.green : Color.white);
            g.drawString(back.text, back.x, back.y);
        }



        g.setColor(Color.black);
        g.drawString("Spielfeldlänge", 825, 25);
        minus1.render(gc, g);
        plus1.render(gc, g);
        g.drawRect(850, 50, 36, 36);
        g.drawRect(950, 50, 36, 36);
        g.drawString(String.valueOf(this.fieldSizeX), 900, 58);
        g.drawString("Spielfeldbreite", 825, 100);
        minus2.render(gc, g);
        plus2.render(gc, g);
        g.drawRect(850, 125, 36, 36);
        g.drawRect(950, 125, 36, 36);
        g.drawString(String.valueOf(this.fieldSizeY), 900, 133);
        g.drawString("Torgröße", 825, 175);
        minus3.render(gc, g);
        plus3.render(gc, g);
        g.drawRect(850, 200, 36, 36);
        g.drawRect(950, 200, 36, 36);
        g.drawString(String.valueOf(this.fieldGoalSize), 900, 208);
        g.drawString("PowerUp - %", 825, 250);
        minus4.render(gc, g);
        plus4.render(gc, g);
        g.drawRect(850, 275, 36, 36);
        g.drawRect(950, 275, 36, 36);
        g.drawString(String.valueOf(this.powerUp), 908, 288);
        g.drawString("PowerDown - %", 825, 325);
        minus5.render(gc, g);
        plus5.render(gc, g);
        g.drawRect(850, 350, 36, 36);
        g.drawRect(950, 350, 36, 36);
        g.drawString(String.valueOf(this.powerDown), 908, 358);
        g.drawString("Hindernisse", 825, 400);
        wallY1x1.render(gc, g);
        wallY2x1.render(gc, g);
        wallY1x2.render(gc, g);
        wallY2x2.render(gc, g);
        g.drawString("Hintergrund", 825, 475);
        colorwhite.render(gc, g);
        colorblue.render(gc, g);
        colorred.render(gc, g);
        coloryellow.render(gc, g);
        colorgreen.render(gc, g);
        colorblack.render(gc, g);
        colorpink.render(gc, g);
        colorgray.render(gc, g);
        g.drawRect(850, 500, 96, 48);
        eraser.render(gc, g);
        g.drawRect(964, 500, 24, 24);
        g.drawString("Map speichern", 825, 570);
        textField.render(gc, g);
        save.render(gc, g);
        g.drawString("Neu / Map laden", 825, 660);
        newMap.render(gc, g);
        load.render(gc, g);
        g.drawRect(900, 690, 32, 32);

        if (!mode) {
            g.setColor(Color.lightGray);
            for (int x = 0; x < 816; x += 24) {
                g.drawLine(0, x, 816, x);
                g.drawLine(x, 0, x, 768);
            }

            this.drawField(g, this.fieldSizeX, this.fieldSizeY, this.fieldGoalSize);

            if (hinderniss[0] == true) {
                g.drawImage(wall1x1, gc.getInput().getMouseX() - 12, gc.getInput().getMouseY() - 12);
            }
            if (hinderniss[1] == true) {
                g.drawImage(wall2x1, gc.getInput().getMouseX() - 12, gc.getInput().getMouseY() - 12);
            }
            if (hinderniss[2] == true) {
                g.drawImage(wall1x2, gc.getInput().getMouseX() - 12, gc.getInput().getMouseY() - 12);
            }
            if (hinderniss[3] == true) {
                g.drawImage(wall2x2, gc.getInput().getMouseX() - 12, gc.getInput().getMouseY() - 12);
            }
            if (hinderniss[4] == true) {
                g.drawImage(eraserIcon, gc.getInput().getMouseX() - 12, gc.getInput().getMouseY() - 12);
            }

            if (speicherFehler[0] == true) {
                g.setColor(Color.black);
                g.drawString("...gespeichert!", 890, 630);
            }

            if (speicherFehler[1] == true) {
                g.setColor(Color.red);
                g.drawString("Mapname", 890, 620);
                g.drawString("eingeben!!", 890, 630);
            }

            if (speicherFehler[2] == true) {
                g.drawString("Fehler beim Speichern", 890, 630);
            }

            for (int i = 0; i < 34; i++) {
                for (int j = 0; j < 32; j++) {
                    if (map[i][j] == 1) {
                        g.drawImage(wall1x1, (i * 24) + 1, (j * 24) + 1);
                    }
                }
            }
        }
    }

    /**
     * Update the LevelEditor
     *
     * @param gc
     * @param sbg
     * @param i
     * @throws SlickException
     */
    @Override
    public void update(GameContainer gc, StateBasedGame sbg, int i) throws SlickException {


        // Handle the Input
        Input input = gc.getInput();
        mouseX = input.getMouseX();
        mouseY = input.getMouseY();

        if (mode) {
            if (input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {
                if (back.checkMouseIn(mouseX, mouseY)) {
                    back.clickPause();
                    mode = false;
                }

                for (int k = 0; k < MapEntries.length; k++) {
                    if (MapEntries[k] != null) {
                        if (!MapEntries[k].text.contains("default")) {
                            if (MapEntries[k].checkMouseIn(mouseX, mouseY)) {
                                try {
                                    this.loadMap(new File("maps\\" + MapEntries[k].text));
                                } catch (FileNotFoundException ex) {
                                    Logger.getLogger(LevelEditor.class.getName()).log(Level.SEVERE, null, ex);
                                } catch (IOException ex) {
                                    Logger.getLogger(LevelEditor.class.getName()).log(Level.SEVERE, null, ex);
                                }
                                mode = false;
                            }
                        }
                    }
                }

            }

            if (input.isKeyDown(Input.KEY_ESCAPE)) {
                try {
                    Thread.sleep(250);
                } catch (InterruptedException ex) {
                    Logger.getLogger(GamePlay.class.getName()).log(Level.SEVERE, null, ex);
                }
                mode = false;
                return;
            }


        }

        if (!mode) {
            if (input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {

                if (save.isMouseOver()) {
                    try {
                        this.saveMap();
                    } catch (IOException ex) {
                        speicherFehler[2] = true;
                    }
                }

                if (load.isMouseOver()) {
                    mode = true;
                }

                if (hinderniss[0] == true && mouseX < 816 && mouseY < 768) {
                    mapX = mouseX / 24;
                    mapY = mouseY / 24;
                    map[mapX][mapY] = 1;
                }
                if (hinderniss[1] == true && mouseX < 816 && mouseY < 768) {
                    mapX = mouseX / 24;
                    mapY = mouseY / 24;
                    map[mapX][mapY] = 1;
                    map[mapX + 1][mapY] = 1;
                }
                if (hinderniss[2] == true && mouseX < 816 && mouseY < 768) {
                    mapX = mouseX / 24;
                    mapY = mouseY / 24;
                    map[mapX][mapY] = 1;
                    map[mapX][mapY + 1] = 1;
                }
                if (hinderniss[3] == true && mouseX < 816 && mouseY < 768) {
                    mapX = mouseX / 24;
                    mapY = mouseY / 24;
                    map[mapX][mapY] = 1;
                    map[mapX][mapY + 1] = 1;
                    map[mapX + 1][mapY] = 1;
                    map[mapX + 1][mapY + 1] = 1;
                }
                if (hinderniss[4] == true && mouseX < 816 && mouseY < 768) {
                    mapX = mouseX / 24;
                    mapY = mouseY / 24;
                    map[mapX][mapY] = 0;
                }

                if (wallY1x1.isMouseOver()) {
                    hinderniss[0] = true;
                    hinderniss[1] = false;
                    hinderniss[2] = false;
                    hinderniss[3] = false;
                    hinderniss[4] = false;
                }
                if (wallY2x1.isMouseOver()) {
                    hinderniss[0] = false;
                    hinderniss[1] = true;
                    hinderniss[2] = false;
                    hinderniss[3] = false;
                    hinderniss[4] = false;
                }
                if (wallY1x2.isMouseOver()) {
                    hinderniss[0] = false;
                    hinderniss[1] = false;
                    hinderniss[2] = true;
                    hinderniss[3] = false;
                    hinderniss[4] = false;
                }
                if (wallY2x2.isMouseOver()) {
                    hinderniss[0] = false;
                    hinderniss[1] = false;
                    hinderniss[2] = false;
                    hinderniss[3] = true;
                    hinderniss[4] = false;
                }
                if (eraser.isMouseOver()) {
                    hinderniss[0] = false;
                    hinderniss[1] = false;
                    hinderniss[2] = false;
                    hinderniss[3] = false;
                    hinderniss[4] = true;
                }
                if (minus1.isMouseOver()) {
                    if (this.fieldSizeX > 200) {
                        this.fieldSizeX--;
                    }
                }
                if (plus1.isMouseOver()) {
                    if (this.fieldSizeX < 675) {
                        this.fieldSizeX++;
                    }
                }
                if (minus2.isMouseOver()) {
                    if (this.fieldSizeY > (this.fieldGoalSize + 50)) {
                        this.fieldSizeY--;
                    }
                }
                if (plus2.isMouseOver()) {
                    if (this.fieldSizeY < 768) {
                        this.fieldSizeY++;
                    }
                }
                if (minus3.isMouseOver()) {
                    if (this.fieldGoalSize > 50) {
                        this.fieldGoalSize--;
                    }
                }
                if (plus3.isMouseOver()) {
                    if (this.fieldGoalSize < (this.fieldSizeY - 50)) {
                        this.fieldGoalSize++;
                    }
                }
                if (minus4.isMouseOver()) {
                    if (this.powerUp > 0) {
                        this.powerUp--;
                    }
                }
                if (plus4.isMouseOver()) {
                    if (this.powerUp < 100) {
                        this.powerUp++;
                    }
                }
                if (minus5.isMouseOver()) {
                    if (this.powerDown > 0) {
                        this.powerDown--;
                    }
                }
                if (plus5.isMouseOver()) {
                    if (this.powerDown < 100) {
                        this.powerDown++;
                    }
                }
                if (colorwhite.isMouseOver()) {
                    background = Color.white;
                }
                if (colorblack.isMouseOver()) {
                    background = Color.black;
                }
                if (colorgray.isMouseOver()) {
                    background = Color.gray;
                }
                if (colorgreen.isMouseOver()) {
                    background = Color.green;
                }
                if (coloryellow.isMouseOver()) {
                    background = Color.yellow;
                }
                if (colorred.isMouseOver()) {
                    background = Color.red;
                }
                if (colorpink.isMouseOver()) {
                    background = Color.pink;
                }
                if (colorblue.isMouseOver()) {
                    background = Color.blue;
                }

                if (newMap.isMouseOver()) {
                    try {
                        this.loadMap(new File("maps\\default.txt"));
                    } catch (FileNotFoundException ex) {
                        Logger.getLogger(LevelEditor.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (IOException ex) {
                        Logger.getLogger(LevelEditor.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }


            if (input.isMouseButtonDown(input.MOUSE_RIGHT_BUTTON)) {

                for (int l = 0; l < hinderniss.length; l++) {
                    hinderniss[l] = false;
                }
            }

            if (input.isKeyDown(Input.KEY_ESCAPE)) {
                try {
                    Thread.sleep(250);
                } catch (InterruptedException ex) {
                    Logger.getLogger(GamePlay.class.getName()).log(Level.SEVERE, null, ex);
                }
                sbg.enterState(bungabungaball.BungaBungaBall.MAINMENUSTATE);
            }
        }


    }

    /**
     * Read out the names of saved maps
     *
     * @param gc
     */
    public void readOutFiles(GameContainer gc) {

        File f = new File("maps\\");
        files = f.listFiles();

        int y = 100;
        y += 25;
        if (files != null) { // Erforderliche Berechtigungen etc. sind vorhanden

            for (int i = 0; i < files.length; i++) {
                y += 25;
                MapEntries[i] = new MenuEntry(files[i].getName(), 100, y, gc.getDefaultFont());
                if (files[i].getName().equals("defaultGamePlay.txt") || files[i].getName().equals("default.txt")) {
                    MapEntries[i].text = files[i].getName() + " (nicht editierbar)";
                }
            }
        }
        back = new MenuEntry("Zurück zum Editor", 100, y + 50, gc.getDefaultFont());
    }

    /**
     * Draw the field where you can create your map
     *
     * @param g
     * @param sizex
     * @param sizey
     * @param goalsizey
     */
    public void drawField(Graphics g, float sizex, float sizey, float goalsizey) {


        if (background == Color.black || background == Color.blue) {
            lineColor = Color.white;
            g.setColor(lineColor);
        } else {
            lineColor = Color.black;
            g.setColor(lineColor);
        }

        g.setLineWidth(3);
        g.drawLine(mittelpunktX - (sizex / 2), mittelpunktY - (sizey / 2), mittelpunktX + (sizex / 2), mittelpunktY - (sizey / 2));//Seitenlinie oben
        g.drawLine(mittelpunktX - (sizex / 2), mittelpunktY + (sizey / 2), mittelpunktX + (sizex / 2), mittelpunktY + (sizey / 2));//Seitenlinie unten
        g.drawLine(mittelpunktX - (sizex / 2) - 25, mittelpunktY - (sizey / 2) + 25, mittelpunktX - (sizex / 2) - 25, mittelpunktY + (sizey / 2) - 25); // Seitenlinie links
        g.drawLine(mittelpunktX + (sizex / 2) + 25, mittelpunktY - (sizey / 2) + 25, mittelpunktX + (sizex / 2) + 25, mittelpunktY + (sizey / 2) - 25); // Seitenlinie rechts
        g.drawLine(mittelpunktX, mittelpunktY - (sizey / 2), mittelpunktX, mittelpunktY + (sizey / 2));
        g.drawRect(mittelpunktX - (sizex / 2) - 65, mittelpunktY - (goalsizey / 2), 40, goalsizey);//Tor links
        g.drawRect(mittelpunktX + (sizex / 2) + 25, mittelpunktY - (goalsizey / 2), 40, goalsizey);//Tor rechts
        g.drawLine(mittelpunktX - (sizex / 2) - 25, mittelpunktY - (sizey / 2) + 25, mittelpunktX - (sizex / 2), mittelpunktY - (sizey / 2)); // Ecken
        g.drawLine(mittelpunktX + (sizex / 2), mittelpunktY - (sizey / 2), mittelpunktX + (sizex / 2) + 25, mittelpunktY - (sizey / 2) + 25);
        g.drawLine(mittelpunktX - (sizex / 2) - 25, mittelpunktY + (sizey / 2) - 25, mittelpunktX - (sizex / 2), mittelpunktY + (sizey / 2));
        g.drawLine(mittelpunktX + (sizex / 2), mittelpunktY + (sizey / 2), mittelpunktX + (sizex / 2) + 25, mittelpunktY + (sizey / 2) - 25);
        g.drawOval(mittelpunktX - (0.15f * sizex), mittelpunktY - (0.15f * sizex), 0.3f * sizex, 0.3f * sizex);
        g.setLineWidth(11);
        g.drawLine(mittelpunktX, mittelpunktY, mittelpunktX, mittelpunktY);
        g.setLineWidth(1);
    }

    /**
     * Create default map
     *
     */
    public void fillDefaultMap() {

        map = new int[40][40];
        for (int i = 0; i < 34; i++) {
            for (int j = 0; j < 32; j++) {
                map[i][j] = 0;
            }
        }
    }

    /**
     * Save a map
     *
     * @throws IOException
     */
    public void saveMap() throws IOException {

        speicherFehler[0] = false;
        speicherFehler[1] = false;
        speicherFehler[2] = false;

        if (!textField.getText().equals("")) {
            File datei = new File("maps\\" + textField.getText() + ".txt");
            BufferedWriter writer = new BufferedWriter(new FileWriter(datei));
            writer.write(String.valueOf(this.fieldSizeX));
            writer.newLine();
            writer.write(String.valueOf(this.fieldSizeY));
            writer.newLine();
            writer.write(String.valueOf(this.fieldGoalSize));
            writer.newLine();
            writer.write(String.valueOf(this.powerUp));
            writer.newLine();
            writer.write(String.valueOf(this.powerDown));
            writer.newLine();
            writer.write(String.valueOf(background.getBlue()));
            writer.newLine();
            writer.write(String.valueOf(background.getGreen()));
            writer.newLine();
            writer.write(String.valueOf(background.getRed()));
            writer.newLine();
            writer.write(String.valueOf(lineColor.getBlue()));
            writer.newLine();
            writer.write(String.valueOf(lineColor.getGreen()));
            writer.newLine();
            writer.write(String.valueOf(lineColor.getRed()));
            writer.newLine();
            for (int i = 0; i < 34; i++) {
                for (int j = 0; j < 32; j++) {
                    writer.write(String.valueOf(map[i][j]));
                }
                writer.newLine();
            }
            writer.close();
            speicherFehler[0] = true;
        } else {
            speicherFehler[1] = true;
        }
    }

    /**
     * Load a map
     *
     * @param fileObj
     * @throws FileNotFoundException
     * @throws IOException
     */
    public void loadMap(File fileObj) throws FileNotFoundException, IOException {

        BufferedReader reader = new BufferedReader(new FileReader(fileObj));
        String s;
        if ((s = reader.readLine()) != null) {
            this.fieldSizeX = Integer.valueOf(s);
        }
        if ((s = reader.readLine()) != null) {
            this.fieldSizeY = Integer.valueOf(s);
        }
        if ((s = reader.readLine()) != null) {
            this.fieldGoalSize = Integer.valueOf(s);
        }
        if ((s = reader.readLine()) != null) {
            this.powerUp = Integer.valueOf(s);
        }
        if ((s = reader.readLine()) != null) {
            this.powerDown = Integer.valueOf(s);
        }
        if ((s = reader.readLine()) != null) {
            int blue = 0;
            int green = 0;
            int red = 0;
            blue = Integer.valueOf(s);
            if ((s = reader.readLine()) != null) {
                green = Integer.valueOf(s);
                if ((s = reader.readLine()) != null) {
                    red = Integer.valueOf(s);
                }
            }
            this.background = new Color(red, green, blue);
        }
        if ((s = reader.readLine()) != null) {
            int blue = 0;
            int green = 0;
            int red = 0;
            blue = Integer.valueOf(s);
            if ((s = reader.readLine()) != null) {
                green = Integer.valueOf(s);
                if ((s = reader.readLine()) != null) {
                    red = Integer.valueOf(s);
                }
            }
            this.lineColor = new Color(red, green, blue);
        }
        int i = 0;
        while ((s = reader.readLine()) != null) {
            int value = 0;
            for (int j = 0; j < s.length(); j++) {
                if (s.charAt(j) == 48) {
                    value = 0;
                } else {
                    value = 1;
                }
                this.map[i][j] = value;
            }
            i++;
        }
    }
}
